package com.bosssoft.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bosssoft.entity.dto.FibDepartmentDTO;
import com.bosssoft.entity.po.FibDepartmentPO;
import com.bosssoft.entity.vo.FibDepartmentVO;
import com.bosssoft.enums.CopyPropertityEnum;
import com.bosssoft.exception.DaoException;
import com.bosssoft.mapper.FibDepartmentMapper;
import com.bosssoft.page.PageResult;
import com.bosssoft.service.FibDepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static org.apache.poi.hssf.usermodel.HeaderFooter.page;

/**
 * 部门表(FibDepartment)表服务实现类
 *
 * @author makejava
 * @since 2021-08-20 10:59:15
 */
@Service("fibDepartmentService")
@Slf4j
public class FibDepartmentServiceImpl extends ServiceImpl<FibDepartmentMapper, FibDepartmentPO> implements FibDepartmentService {
    @Resource
    private FibDepartmentMapper fibDepartmentMapper;

    @Override
    public FibDepartmentDTO selectOneById(Long id) {
        try {
            FibDepartmentPO fibDepartmentPO = this.fibDepartmentMapper.selectById(id);
            FibDepartmentDTO fibDepartmentDTO = new FibDepartmentDTO();
            BeanUtils.copyProperties(fibDepartmentPO, fibDepartmentDTO);
            return fibDepartmentDTO;
        } catch (Exception e) {
            throw DaoException.error(CopyPropertityEnum.RESOURCE_NULL_ERROR);
        }
    }

    @Override
    public List<FibDepartmentDTO> selectAll(Long page, Long limit) {
        try {
            Long offset = page * 10 - 10;
            if (limit == null) {
                limit = Long.valueOf(10);
            }
            List<FibDepartmentPO> fibDepartmentPOS = this.fibDepartmentMapper.selectAll(offset, limit);
            List<FibDepartmentDTO> fibDepartmentDTOS = new ArrayList<>();
            for (int i = 0; i < fibDepartmentPOS.size(); i++) {
                fibDepartmentDTOS.add(new FibDepartmentDTO());
                BeanUtils.copyProperties(fibDepartmentPOS.get(i), fibDepartmentDTOS.get(i));
            }
            return fibDepartmentDTOS;
        } catch (Exception e) {
            throw DaoException.error(CopyPropertityEnum.RESOURCE_NULL_ERROR);
        }
    }

    /**
     * 新增数据
     *
     * @param
     * @return 实例对象
     */
    @Override
    public boolean insert(FibDepartmentDTO fibDepartmentDTO) {
        try {
            FibDepartmentPO fibDepartmentPO = new FibDepartmentPO();
            BeanUtils.copyProperties(fibDepartmentDTO, fibDepartmentPO);
            return this.fibDepartmentMapper.insert(fibDepartmentPO) > 0;
        } catch (Exception e) {
            throw DaoException.error(CopyPropertityEnum.RESOURCE_NULL_ERROR);
        }
    }

    @Override
    public boolean delete(FibDepartmentDTO fibDepartmentDTO) {
        FibDepartmentPO fibDepartmentPO = new FibDepartmentPO();
        BeanUtils.copyProperties(fibDepartmentDTO, fibDepartmentPO);
        return this.fibDepartmentMapper.delete(fibDepartmentPO);
    }

    @Override
    public boolean update(FibDepartmentDTO fibDepartmentDTO) {
        FibDepartmentPO fibDepartmentPO = new FibDepartmentPO();
        BeanUtils.copyProperties(fibDepartmentDTO, fibDepartmentPO);
        return this.fibDepartmentMapper.update(fibDepartmentPO);
    }

    @Override
    public PageResult<FibDepartmentPO> page(Page<FibDepartmentPO> page, FibDepartmentPO fibDepartmentPO) {

        // 构造条件
        QueryWrapper<FibDepartmentPO> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(fibDepartmentPO.getDeptId())) {
            queryWrapper.like("dept_id", fibDepartmentPO.getDeptId());
        }
        if (ObjectUtil.isNotNull(fibDepartmentPO.getDeptName())) {
            queryWrapper.like("dept_name", fibDepartmentPO.getDeptName());
        }
        // 查询分页结果
        return new PageResult<>(this.page(page, queryWrapper));
    }

    @Override
    public boolean updateDeleted(FibDepartmentDTO fibDepartmentDTO) {
        FibDepartmentPO fibDepartmentPO = new FibDepartmentPO();
        BeanUtils.copyProperties(fibDepartmentDTO, fibDepartmentPO);
        return this.fibDepartmentMapper.updateDeleted(fibDepartmentPO);
    }

    @Override
    public List<Long> selectChildrenIds(Long deptId) {
        List<Long> ids = fibDepartmentMapper.selectChildrenIds(deptId);
        return ids;
    }

    @Override
    public Page<FibDepartmentVO> selectChildrenPage(Integer current, Integer size, Long deptId) {

        Page<FibDepartmentPO> page = new Page<>(current, size);
        LambdaQueryWrapper<FibDepartmentPO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(deptId != null, FibDepartmentPO::getDeptCode, deptId);
        Page<FibDepartmentPO> page1 = page(page, wrapper);
        Page<FibDepartmentVO> page2 = new Page<>(current, size);
        BeanUtils.copyProperties(page1, page2);
        return page2;
    }


}



